<style lang="less">
@import '../common/less/varible.less';

.search {
    // padding: @pad;
    box-sizing: border-box;
    .search-top {
        width: 100%;
        display: flex;
        padding: 10px 5px;
        .back {
            height: 40px;
            width: 50px;
            text-align: center;
            line-height: 40px;
            font-size: 18px;
            // font-family: Arial, Helvetica, sans-serif;
        }
        input[type='text'] {
            border: 1px solid @borderColor;
            height: 40px;
            border-top-left-radius: 20px;
            border-bottom-left-radius: 20px;
            padding-left: 15px;
            box-sizing: border-box;
            font-size: 16px;
            width: calc(100% - 110px);
            font-family: Arial, Helvetica, sans-serif;
        }

        input[type='text']::placeholder {
            color: @activeColor;
        }

        button {
            background-color: @bgColor;
            color: #ffffff;
            width: 60px;
            height: 40px;
            font-size: 16px;
            border-top-right-radius: 20px;
            border-bottom-right-radius: 20px;
            font-family: Arial, Helvetica, sans-serif;
        }
    }
    ul.song-list-1 {
        padding: 0 15px;
        margin-top: 10px;
        li {
            a {
                display: flex;
                width: 100%;
                justify-content: space-between;
                align-items: center;
                font-family: Arial, Helvetica, sans-serif;
                padding: 10px 0px;
                border-bottom: 1px solid #f0f0f0;
                .left {
                    display: flex;
                    .img-box {
                        width: 80px;
                        margin-right: 10px;
                        img {
                            width: 100%;
                        }
                    }
                    h5 {
                        font-weight: normal;
                        font-size: 18px;
                        line-height: 175%;
                    }

                    div {
                        font-size: 13px;
                        color: #666;

                        i {
                            display: inline-block;
                            width: 12px;
                            height: 8px;
                            margin-right: 4px;
                            background-image: url(../assets/img/index_icon_2x.png);
                            background-size: 166px 97px;
                        }
                    }
                }

                .right {
                    button {
                        background-color: transparent;
                        color: #ccc;
                    }
                }
            }
        }
    }
}
</style>

<template>
    <div class="search">
        <!-- <h3>搜索</h3> -->
        <div class="search-top">
             <div class="back" @click="goBack">
                <!-- <van-icon name="revoke" /> -->
                 返回
            </div>
            <input @keydown.enter="getInputValue" v-model="text" type="text" placeholder="关键字搜歌">
            <button @click="getInputValue">搜索</button>
        </div>
        <div v-if="isLoading">
            <!-- 使用loading组件 -->
            <my-loading></my-loading>
        </div>
        <div v-else>
            <div  v-if="arr.length == 0">
                <!-- 搜索反馈 -->
                <van-empty description="暂无搜索列表" />

                <!-- 编辑推荐 -->
                <!-- 推荐音乐 -->
                <recommad-music></recommad-music>
            </div>
            <!-- 搜索列表 -->
            <ul class="song-list-1" v-else>
                <li v-for="(item) in arr" :key="item.id">
                    <router-link :to="{ path: '/detail', query: { song_id: item.song_id , ui:'search',text: text} }">
                        <div class="left">
                            <div class="img-box"><img v-if="item.song_cover_url" :src="item.song_cover_url" alt=""></div>
                           <div>
                                <h5>{{ item.song_name }}</h5>
                                <div v-if="item.singer_name">
                                    <span >{{ item?.singer_name }} </span> | 
                                    <span >{{ item?.category_name }} </span>
                                </div>
                           </div>
                        </div>
                        <div class="right">
                            <button>
                                <van-icon name="play-circle-o" />
                            </button>
                        </div>
                    </router-link>
                </li>
            </ul>
        </div>

    </div>
</template>


<script>
// 导入组件
import { Toast } from 'vant'
import RecommadMusic from '@/components/RecommadMusic.vue';
// 导入自定义组件
import MyLoading from '../components/MyLoading.vue'
// 导入接口
// import { songSearch , host } from '../api/index'
export default {
    // 注册组件
    components: {
        MyLoading,
        RecommadMusic
    },

    data() {
        return {
            arr: [],
            text: "", // 记录输入框的值,
            isLoading: false,
        }
    },

    methods: {
        // 自定义方法
        async songSearchFn(value) {
            // console.log({keyword: value})
            // 请求之前 显示loading
            this.isLoading = true;
            // 调用搜索接口
            // await songSearch({ keyword: value })
            //     .then(
            //         content => {
            //             console.log("请求成功:",content)
            //             // 解构赋值
            //             let { code, result } = content;
            //             // 判断数据是否为目标数据
            //             if (code == 200) {
            //                 // 整合数据
            //                 let newArr = result.map(item => {
            //                     item.song_cover_url = host+item.song_cover_url
            //                     // 返回每一条整合后的数据
            //                     return item;
            //                 })
            //                 // 赋值
            //                 this.arr = newArr;
            //             }
            //         }
            //     )
            //     .catch(
            //         err => {
            //             console.log("请求失败:", err)
            //         }
            //     )
            await this.$store.dispatch('music/searchSongFn', { keyword: value });
            this.arr = this.$store.state.music.songs;
            // 请求之后
            setTimeout(() => {
                // 隐藏loading
                this.isLoading = false;
            }, 500);
        },

        // 获取输入框输入的值
        getInputValue() {
            // 获取输入框的值
            let text = this.text;
            // 判断输入框是否为空
            if (text == "" || text.length == 0) {
                Toast("请输入关键字");
                return false;
            }

            // 检查代码是否执行到此处
            // console.log({text});

            // 搜素歌曲
            this.songSearchFn(text);
        },

        // 返回上一级历史
        goBack(){
            this.$router.go(-1);
        }
        
    },
}
</script>